

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="caoyusang">
  <meta name="keywords" content="">
  <title>手撕排序算法 - caoyusang的科研日常</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/github-gist.min.css" />
    
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.2.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>caoyusang</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/img/7.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container page-header text-center fade-in-up">
            <span class="h2" id="subtitle">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-05-03 15:56" pubdate>
        2021年5月3日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.6k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      27
       分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto" id="post">
            <!-- SEO header -->
            <h1 style="display: none">手撕排序算法</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：25 天前
                
              </p>
            
            <div class="markdown-body" id="post-body">
              <h2 id="手撕排序算法"><a href="#手撕排序算法" class="headerlink" title="手撕排序算法"></a>手撕排序算法</h2><h3 id="冒泡排序"><a href="#冒泡排序" class="headerlink" title="冒泡排序"></a>冒泡排序</h3><p>时间复杂度O(n*n)，空间复杂度O(n)，稳定排序</p>
<img src="/2021/05/03/%E6%89%8B%E6%92%95%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/20190803104032665.gif" srcset="/img/loading.gif" class="">
<pre><code class="hljs java"><span class="hljs-comment">//冒泡排序——每次把数组无序部分的最大值交换到数组最末尾</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">bubbleSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] nums)</span></span>&#123;
    <span class="hljs-keyword">int</span> len = nums.length;
    <span class="hljs-comment">//记录上一次最后完成交换的位置，因为后面的已经有序了，这样可以避免无意义的比较判断</span>
    <span class="hljs-keyword">int</span> lastSwap = len;
    <span class="hljs-comment">//外循环——冒泡交换的趟数</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; len; i++)&#123;
        <span class="hljs-comment">//用于判断该趟有无元素交换，没有就说明数组已经有序了，可以break跳出循环</span>
        <span class="hljs-keyword">boolean</span> hasSwap = <span class="hljs-keyword">false</span>;
        <span class="hljs-comment">//暂存需要比较到的位置</span>
        <span class="hljs-keyword">int</span> sortBorder = lastSwap;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">1</span>; j &lt; sortBorder; j++)&#123;
            <span class="hljs-keyword">if</span> (nums[j] &lt; nums[j - <span class="hljs-number">1</span>])&#123;
                <span class="hljs-comment">//进行元素交换</span>
                <span class="hljs-keyword">int</span> temp = nums[j];
                nums[j] = nums[j - <span class="hljs-number">1</span>];
                nums[j - <span class="hljs-number">1</span>] = temp;
                <span class="hljs-comment">//发生交换</span>
                hasSwap = <span class="hljs-keyword">true</span>;
                <span class="hljs-comment">//更新最后交换元素的位置</span>
                lastSwap = j;
            &#125;
        &#125;
        <span class="hljs-comment">//如果该趟没有发生元素交换，直接跳出循环</span>
        <span class="hljs-keyword">if</span> (!hasSwap) <span class="hljs-keyword">break</span>;
    &#125;
&#125;</code></pre>
<h3 id="选择排序"><a href="#选择排序" class="headerlink" title="选择排序"></a>选择排序</h3><p>时间复杂度O(n*n)，空间复杂度O(n)，不稳定排序</p>
<img src="/2021/05/03/%E6%89%8B%E6%92%95%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/20190803104104133.gif" srcset="/img/loading.gif" class="">
<pre><code class="hljs java"><span class="hljs-comment">//选择排序——找出无序部分中的最小值，放到有序部分的末尾</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">selectSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] nums)</span></span>&#123;
    <span class="hljs-keyword">int</span> n = nums.length;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; n; i++)&#123;
        <span class="hljs-keyword">int</span> min = i;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = i + <span class="hljs-number">1</span>; j &lt; n; j++)&#123;
            <span class="hljs-comment">//找当前数组无序部分的最小值的下标，将对应的数放到有序部分末尾</span>
            <span class="hljs-keyword">if</span> (nums[j] &lt; nums[min])&#123;
                min = j;
            &#125;
        &#125;
        <span class="hljs-comment">//做交换</span>
        <span class="hljs-keyword">int</span> temp = nums[min];
        nums[min] = nums[i];
        nums[i] = temp;
    &#125;
&#125;</code></pre>
<h3 id="插入排序"><a href="#插入排序" class="headerlink" title="插入排序"></a>插入排序</h3><p>时间复杂度O(n*n)，空间复杂度O(1)，稳定排序</p>
<img src="/2021/05/03/%E6%89%8B%E6%92%95%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/20190803104128360.gif" srcset="/img/loading.gif" class="">
<pre><code class="hljs java"><span class="hljs-comment">//插入排序，将数组无序部分的元素向前插入到数组有序部分的对应位置</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">insertSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] nums)</span></span>&#123;
    <span class="hljs-keyword">int</span> n = nums.length;
    <span class="hljs-comment">//第一个元素无法向前插入</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; n; i++)&#123;
        <span class="hljs-comment">//待插入元素</span>
        <span class="hljs-keyword">int</span> val = nums[i];
        <span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>;
        <span class="hljs-keyword">for</span> (j = i - <span class="hljs-number">1</span>; j &gt;= <span class="hljs-number">0</span>; j--)&#123;
            <span class="hljs-comment">//待插入元素比前一个元素小，则让前一个元素覆盖它，继续向前查找插入位置，找到就break</span>
            <span class="hljs-keyword">if</span> (val &lt; nums[j])&#123;
                nums[j + <span class="hljs-number">1</span>] = nums[j];
            &#125; <span class="hljs-keyword">else</span> &#123;
                <span class="hljs-keyword">break</span>;
            &#125;
        &#125;
        <span class="hljs-comment">//插入元素</span>
        nums[j + <span class="hljs-number">1</span>] = val;
    &#125;
&#125;</code></pre>
<h3 id="快速排序"><a href="#快速排序" class="headerlink" title="快速排序"></a>快速排序</h3><p>时间复杂度O(n*log(n))，空间复杂度O(log(n))，不稳定排序</p>
<img src="/2021/05/03/%E6%89%8B%E6%92%95%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/20190803104243572.gif" srcset="/img/loading.gif" class="">
<pre><code class="hljs java"><span class="hljs-comment">//递归快排</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">quickSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span> start, <span class="hljs-keyword">int</span> end, <span class="hljs-keyword">int</span>[] nums)</span></span>&#123;
    <span class="hljs-keyword">if</span> (start &gt;= end) <span class="hljs-keyword">return</span>;
    <span class="hljs-keyword">int</span> border = partion(start, end, nums);
    quickSort(start, border - <span class="hljs-number">1</span>, nums);
    quickSort(border + <span class="hljs-number">1</span>, end, nums);
&#125;

<span class="hljs-comment">//快排——双边扫描，把比基准元素放到正确的位置，基准元素左边全是比它小的，右边全是比它大的</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">partion</span><span class="hljs-params">(<span class="hljs-keyword">int</span> start, <span class="hljs-keyword">int</span> end, <span class="hljs-keyword">int</span>[] nums)</span></span>&#123;
    <span class="hljs-keyword">int</span> left = start, right = end;
    <span class="hljs-comment">//选取当前分区第一个元素作为基准元素(可以有别的选取基准元素的方法)</span>
    <span class="hljs-keyword">int</span> pivot = nums[start];
    <span class="hljs-keyword">while</span> (left != right)&#123;
        <span class="hljs-comment">//右哨兵right向左搜索第一个比pivot小的元素</span>
        <span class="hljs-keyword">while</span>(nums[right] &gt;= pivot &amp;&amp; left &lt; right) right--;
        <span class="hljs-comment">//左哨兵left向右搜索第一个比pivot大的元素</span>
        <span class="hljs-keyword">while</span>(nums[left] &lt;= pivot &amp;&amp; left &lt; right) left++;

        <span class="hljs-comment">//如果一轮搜索完毕后，left &lt; right，将nums[left]与nums[right]互换，使得小的元素在前，大的元素在后</span>
        <span class="hljs-keyword">if</span> (left &lt; right)&#123;
            swap(nums, left, right);
        &#125;
    &#125;
    <span class="hljs-comment">//将基准元素放到对的位置，即此时将nums[start]和nums[left]互换</span>
    nums[start] = nums[left];
    nums[left] = pivot;
    <span class="hljs-comment">//返回基准元素的位置，作为分区的界限</span>
    <span class="hljs-keyword">return</span> left;
&#125;

<span class="hljs-comment">//用于数组的元素交换</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">swap</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] arr, <span class="hljs-keyword">int</span> i, <span class="hljs-keyword">int</span> j)</span></span>&#123;
    <span class="hljs-keyword">int</span> temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
&#125;</code></pre>
<h3 id="归并排序"><a href="#归并排序" class="headerlink" title="归并排序"></a>归并排序</h3><p>时间复杂度O(n*log(n))，空间复杂度O(n)，稳定排序</p>
<img src="/2021/05/03/%E6%89%8B%E6%92%95%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/20190803104213505.gif" srcset="/img/loading.gif" class="">
<pre><code class="hljs java"><span class="hljs-comment">//分治</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">divideMerge</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] nums, <span class="hljs-keyword">int</span> left, <span class="hljs-keyword">int</span> right, <span class="hljs-keyword">int</span>[] temp)</span></span>&#123;
    <span class="hljs-keyword">if</span> (left &lt; right)&#123;
        <span class="hljs-keyword">int</span> mid = (left + right) / <span class="hljs-number">2</span>;
        <span class="hljs-comment">//递归</span>
        divideMerge(nums, left, mid, temp);
        divideMerge(nums, mid + <span class="hljs-number">1</span>, right, temp);
        <span class="hljs-comment">//合并结果</span>
        merge(nums, left, mid, right, temp);
    &#125;
&#125;
    
<span class="hljs-comment">//merge</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">merge</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] nums, <span class="hljs-keyword">int</span> left, <span class="hljs-keyword">int</span> mid, <span class="hljs-keyword">int</span> right, <span class="hljs-keyword">int</span>[] temp)</span></span>&#123;
    <span class="hljs-comment">//左半部分的数组指针</span>
    <span class="hljs-keyword">int</span> i = left;
    <span class="hljs-comment">//右半部分的数组指针</span>
    <span class="hljs-keyword">int</span> j = mid + <span class="hljs-number">1</span>;
    <span class="hljs-comment">//合并的临时数组指针</span>
    <span class="hljs-keyword">int</span> t = <span class="hljs-number">0</span>;
    
    <span class="hljs-comment">//合并过程</span>
    <span class="hljs-keyword">while</span> (i &lt;= mid &amp;&amp; j &lt;= right)&#123;
        <span class="hljs-keyword">if</span> (nums[i] &lt;= nums[j])&#123;
            temp[t++] = nums[i++];
        &#125; <span class="hljs-keyword">else</span> &#123;
            temp[t++] = nums[j++];
        &#125;
    &#125;
    
    <span class="hljs-comment">//比较合并完如果还有剩余，则将剩余的元素直接进行填充</span>
    <span class="hljs-keyword">while</span> (i &lt;= mid)&#123;
        temp[t++] = nums[i++];
    &#125;
    <span class="hljs-keyword">while</span> (j &lt;= right)&#123;
        temp[t++] = nums[j++];
    &#125;
    
    <span class="hljs-comment">//将临时数组拷贝到原始数组中，先将临时数组的指针置零</span>
    t = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">while</span> (left &lt;= right)&#123;
        nums[left++] = temp[t++];
    &#125;
&#125;</code></pre>
<h3 id="堆排序"><a href="#堆排序" class="headerlink" title="堆排序"></a>堆排序</h3><p>时间复杂度O(n*log(n))，空间复杂度O(1)，不稳定排序</p>
<img src="/2021/05/03/%E6%89%8B%E6%92%95%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/20190803104341817.gif" srcset="/img/loading.gif" class="">
<p>将待排序序列构造成一个大顶堆，此时，整个序列的最大值就是堆顶的根节点。将其与末尾元素进行交换，此时末尾就为最大值。然后将剩余n-1个元素重新构造成一个堆，这样会得到n个元素的次小值。如此反复执行，便能得到一个有序序列。</p>
<pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">heapSort</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] nums)</span></span>&#123;
    <span class="hljs-comment">//从第一个非叶子节点(0 ~ nums.length - 1编号)开始，自下而上，自右向左调整结构，使之成为最大堆</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = nums.length / <span class="hljs-number">2</span> - <span class="hljs-number">1</span>; i &gt;= <span class="hljs-number">0</span>; i--)&#123;
        adjustHeap(nums, i, nums.length);
    &#125;
    <span class="hljs-comment">//将堆顶元素和数组末尾元素进行交换，然后针对剩下的n-1个元素再次进行堆调整</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = nums.length - <span class="hljs-number">1</span>; j &gt;= <span class="hljs-number">0</span>; j--)&#123;
        swap(nums, <span class="hljs-number">0</span>, j);
        adjustHeap(nums, <span class="hljs-number">0</span>, j);
    &#125;
&#125;

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">adjustHeap</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] nums, <span class="hljs-keyword">int</span> i, <span class="hljs-keyword">int</span> len)</span></span>&#123;
    <span class="hljs-comment">//暂存当前元素</span>
    <span class="hljs-keyword">int</span> temp = nums[i];
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> k = <span class="hljs-number">2</span> * i + <span class="hljs-number">1</span>; k &lt; len; k = k * <span class="hljs-number">2</span> + <span class="hljs-number">1</span>)&#123;
        <span class="hljs-comment">//当前节点的左子节点的值小于右子节点</span>
        <span class="hljs-keyword">if</span> (k &lt; len - <span class="hljs-number">1</span> &amp;&amp; nums[k] &lt; nums[k + <span class="hljs-number">1</span>])&#123;
            k++;
        &#125;
        <span class="hljs-comment">//子节点比父节点大，把子节点的值赋值给父节点就行，因为已经对当前父节点进行了暂存</span>
        <span class="hljs-keyword">if</span> (nums[k] &gt; temp)&#123;
            nums[i] = nums[k];
            i = k;
        &#125; <span class="hljs-keyword">else</span> &#123;
            <span class="hljs-keyword">break</span>;
        &#125;
        
    &#125;
    <span class="hljs-comment">//把之前的父节点放到调整后的位置上</span>
    nums[i] = temp;
&#125;

<span class="hljs-comment">//数组元素交换</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">swap</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] arr, <span class="hljs-keyword">int</span> a, <span class="hljs-keyword">int</span> b)</span></span>&#123;
    <span class="hljs-keyword">int</span> temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp;
&#125;</code></pre>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/chengxiao/p/6129630.html">图解堆排序</a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/as02446418/article/details/47699547">深入浅出堆排序</a></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E7%AE%97%E6%B3%95/">算法</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E9%9D%A2%E8%AF%95%E9%A2%98/">面试题</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/05/03/TX%E7%AC%94%E8%AF%95/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">TX笔试</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/05/03/JD%E7%AC%94%E8%AF%95/">
                        <span class="hidden-mobile">JD笔试</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: '#post-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "手撕排序算法&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>





  

  
    <!-- MathJax -->
    <script>
      MathJax = {
        tex: {
          inlineMath: [['$', '$'], ['\\(', '\\)']]
        },
        options: {
          renderActions: {
            findScript: [10, doc => {
              document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
                const display = !!node.type.match(/; *mode=display/);
                const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
                const text = document.createTextNode('');
                node.parentNode.replaceChild(text, node);
                math.start = { node: text, delim: '', n: 0 };
                math.end = { node: text, delim: '', n: 0 };
                doc.math.push(math);
              });
            }, '', false],
            insertedScript: [200, () => {
              document.querySelectorAll('mjx-container').forEach(node => {
                let target = node.parentNode;
                if (target.nodeName.toLowerCase() === 'li') {
                  target.parentNode.classList.add('has-jax');
                }
              });
            }, '', false]
          }
        }
      };
    </script>

    <script async src="https://cdn.staticfile.org/mathjax/3.0.5/es5/tex-svg.js" ></script>

  











</body>
</html>
